/**
 *
 */
package com.eeepay.eeepay_v2.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.model.UserInfo;
import com.eeepay.eeepay_v2.util.ConfigPorperties;
import com.eeepay.eeepay_v2.util.SPUtils;
import com.eeepay.v2_library.dialog.CustomDialog;
import com.eeepay.v2_library.dialog.DialogUtil;


/**
 * @author div 2015-12-15 TODO
 */
public class TestUpdateIPAct extends Activity implements RadioGroup.OnCheckedChangeListener {

    public static final String TEST_KEY = "change_key";
    public static final String TEST_URL = "test_url";//测试
    public static final String TEST_PORT = "test_port";//测试 端口

    public static final String ZSC_URL = "zsc_url";//准生产环境
    public static final String ZSC_PORT = "zsc_port";//准生产 端口

    public static final String ONLINE_URL = "online_url";//线上
    public static final String ONLINE_PORT = "online_port";//线上 端口

    TextView msg;

    TextView url_182;
    TextView port_182;

    //add by zhuangzeqin 2017年10月18日17:36:42 添加准生产环境
    TextView url_zsc;
    TextView port_zsc;

    TextView url_shengchan;
    TextView port_shengchan;

    RadioGroup radiogroup;
    RadioButton rb_182;
    RadioButton rb_zsc;//add by zhuangzeqin 2017年10月18日17:43:07 添加准生产环境
    RadioButton rb_shengchan;


    String str_url_182;
    String str_port_182;

    //add by zhuangzeqin 2017年10月18日17:36:42 添加准生产环境
    String str_url_zsc;
    String str_port_zsc;


    String str_url_shengchan;
    String str_port_shengchan;


    private Button btn_OK;//确定


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_update_ip);
        msg = (TextView) findViewById(R.id.msg);

        url_182 = (TextView) findViewById(R.id.url_182);
        port_182 = (TextView) findViewById(R.id.port_182);
        //add by zhuangzeqin 2017年10月18日17:36:42 添加准生产环境
        url_zsc = (TextView) findViewById(R.id.url_zsc);
        port_zsc = (TextView) findViewById(R.id.port_zsc);


        url_shengchan = (TextView) findViewById(R.id.url_shengchan);
        port_shengchan = (TextView) findViewById(R.id.port_shengchan);
        btn_OK = (Button) findViewById(R.id.btn_OK);

        radiogroup = (RadioGroup) findViewById(R.id.radiogroup);
        rb_182 = (RadioButton) findViewById(R.id.rb_138);
        rb_zsc = (RadioButton) findViewById(R.id.rb_zsc);
        rb_shengchan = (RadioButton) findViewById(R.id.rb_shengchan);
        radiogroup.setOnCheckedChangeListener(this);

        str_url_182 = url_182.getText().toString();
        str_port_182 = port_182.getText().toString();
        //准生产环境
        str_url_zsc = url_zsc.getText().toString();
        str_port_zsc = port_zsc.getText().toString();

        str_url_shengchan = url_shengchan.getText().toString();
        str_port_shengchan = port_shengchan.getText().toString();

        int index = (int) SPUtils.get(TEST_KEY, 0);

        switch (index) {
            case 0://线上环境（生产环境）
                setMsg(str_url_shengchan, str_port_shengchan);
                rb_shengchan.setChecked(true);
                break;
            case 1://准生产环境
                setMsg(str_url_zsc, str_port_zsc);
                rb_zsc.setChecked(true);
                break;
            case 2://182环境
                setMsg(str_url_182, str_port_182);
                rb_182.setChecked(true);
                break;
        }

        btn_OK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTips();
            }
        });

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_shengchan://线上环境（生产环境）
                setMsg(str_url_shengchan, str_port_shengchan);
                SPUtils.put(TEST_KEY, 0);
                SPUtils.put(ONLINE_URL, str_url_shengchan);
                SPUtils.put(ONLINE_PORT, str_port_shengchan);
                break;
            case R.id.rb_zsc://准生产环境
                setMsg(str_url_zsc, str_port_zsc);
                SPUtils.put(TEST_KEY, 1);
                SPUtils.put(ZSC_URL, str_url_zsc);
                SPUtils.put(ZSC_PORT, str_port_zsc);
                break;
            case R.id.rb_138://182环境
                setMsg(str_url_182, str_port_182);
                SPUtils.put(TEST_KEY, 2);
                SPUtils.put(TEST_URL, str_url_182);
                SPUtils.put(TEST_PORT, str_port_182);
                break;
        }

    }

    /**
     * 显示温馨提示语
     */
    private CustomDialog tipDialog;

    private void showTips() {
        if (tipDialog == null) {
            tipDialog = DialogUtil.getSingleCustomDialog(this, "温馨提醒", "您已经切换当前的环境.请您重启应用以便生效！", "确  定", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    tipDialog.cancel();
                    UserInfo.removeUserInfo();
                    android.os.Process.killProcess(android.os.Process.myPid());
                    System.exit(0);
                    finish();
                }
            });
        }
        if (tipDialog != null && !isFinishing() && !tipDialog.isShowing()) {
            tipDialog.show();
        }
    }


    public void setMsg(String url, String port) {
//		Constant.URL.URL = url;
//		Constant.URL.PORT = port;
        msg.setText("appNo=" + ConfigPorperties.getInstance().getAppNo() + "\n" + "team_id="
                + ConfigPorperties.getInstance().getTeamId() + "\n------当前ip地址信息------\n" + "url=" + url + "\n" + "port="
                + port);
    }

}
